package cn.fantasy.thread;

/**
 * @author ET
 * @description 使用线程的wait方法
 * @date 2023/3/10 15:08
 * @Version V1.0
 */
public class ThreadOrderThree {

    private static Object lock1 = new Object();

    private static Boolean runFinish1 = false;
    private static Boolean runFinish2 = false;

    public static void main(String[] args) {
        final Thread thread1 = new Thread(() -> {
            synchronized (lock1) {
                System.out.println("one");
                runFinish1 = true;
                lock1.notifyAll();
            }
        });

        final Thread thread2 = new Thread(() -> {
            synchronized (lock1) {
                if (!runFinish1) {
                    try {
                        lock1.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } else {
                    System.out.println("two");
                    runFinish2 = true;
                    lock1.notifyAll();
                }
            }
        });

        final Thread thread3 = new Thread(() -> {
            synchronized (lock1) {
                if (!runFinish1 && !runFinish2) {
                    try {
                        lock1.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } else {
                    System.out.println("three");
                }

            }
        });

        thread1.start();
        thread2.start();
        thread3.start();
    }
}
